from flask import Flask, request, jsonify
import json  # 导入 json 模块
from flask import Flask
from flask_cors import CORS

app = Flask(__name__)
CORS(app)  # 在应用上启用 CORS


@app.route('/login', methods=['POST', 'OPTIONS'])
def login():
    if request.method == 'OPTIONS':
        response = app.response_class(
            response=json.dumps({'success': True}),
            status=200,
            mimetype='application/json'
        )
    elif request.method == 'POST':
        data = request.get_json()
        username = data.get('username')
        password = data.get('password')

        if username == "admin" and password == "123":
            response = jsonify({'success': True, 'message': 'Login successful'})
        else:
            response = jsonify({'fail': False, 'message': 'Login fail'})

    return response


if __name__ == '__main__':
    app.run()
